/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityExternalIdentifierInfo;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityExternalIdentifierInfoMapper implements ContextMapper {

  private final Log log = LogFactory.getLog(getClass());
  private String externalIdAttributeName = null;
  private String externalIdentifierTypeCodeAttributeName = null;

//beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityExternalIdentifierInfo keeii = new KimEntityExternalIdentifierInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;
      // currently, this assumes that an entity only has one external identifier (the one aligned
      // with this ldap record.  this is a decent assumption for now, but a configurable
      // option for either doing it this way or mapping multiple external identifiers would be better
      // perhaps some organizations have multi-valued attributes that list external identifiers

      // the entityExternalIdentifierId is a synthetic identifier for the instance of an entity external identifier.
      // this should be unique in the system, but there is no graceful way to do this
      // from an ldap mapping
      keeii.setEntityExternalIdentifierId("1");//is there an ldap attribute for this?
      keeii.setExternalId(LdapUtilities.materialize(context.getStringAttribute(externalIdAttributeName)));
      keeii.setExternalIdentifierTypeCode(LdapUtilities.materialize(context.getStringAttribute(externalIdentifierTypeCodeAttributeName)));
    } catch (Throwable t) {
      log.error("Error in ContextMapper", t);
      String msg = "ContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped LdapKimEntityExternalIdentifier:"
              + "(externalId=" + keeii.getExternalId() + ")");
    }
    return keeii;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getExternalIdAttributeName() {
    return this.externalIdAttributeName;
  }

  public void setExternalIdAttributeName(String attrName) {
    this.externalIdAttributeName = attrName;
  }

  public String getExternalIdentifierTypeCodeAttributeName() {
    return this.externalIdentifierTypeCodeAttributeName;
  }

  public void setExternalIdentifierTypeCodeAttributeName(String attrName) {
    this.externalIdentifierTypeCodeAttributeName = attrName;
  }

  private void validateConfiguration() {
    if (externalIdAttributeName == null) {
      String msg = "The property 'externalIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (externalIdentifierTypeCodeAttributeName == null) {
      String msg = "The property 'externalIdentifierTypeCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods
}
